import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:['人才库','企业服务'],
    activeIndex:0,
    resumeList:[],
    iShidden:true,
    pageSize:1,
    pageNum:7,
    isPull:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSetting({
      success(res){
       if(res.authSetting['scope.userInfo']){
       var u =  wx.getStorageSync('userInfo')
          that.setData({
            userInfo:u
          })   
       }
      }
    })
    var data = {
      page: that.data.pageSize,
      limit: that.data.pageNum,
      search:''
    }
   that.getConsult(data)
   
  },
  getServe:function(){
    var that = this 
    request.get('employment',{},{ noAuth : true}).then(res=>{
        if(res.status==200){
          that.setData({
            name:res.data.name,
            phone:res.data.phone,
            companyName:res.data.company_name,
            type:res.data.type,
            number:res.data.number,
            benefit:res.data.benefit,
            position:res.data.position,
            ability:res.data.ability
          })
        }
    })
  },
  resume:function(){
    var that = this
    if(that.data.userInfo){
      wx.navigateTo({
       url: '../myResume/myResume',
     })
      }else{
       that.setData({
       iShidden:false
      })
      }

  },
  details:function(d){
    var id = d.currentTarget.dataset.id
    wx.navigateTo({
      url: './resumeDetails/resumeDetails?id='+id,
    })
  },
  search_value:function(e){
    var that = this
    var key = e.detail.send_value
    that.setData({
      keyValue:key,
      pageSize:1,
      isPull:true
    })
    var data = {
      page: that.data.pageSize,
      limit: that.data.pageNum,
      search: that.data.keyValue
    }
    this.getConsult(data,'search')
   },
  getConsult:function(data,type){
    var that = this
    if(type=='search'){
      that.setData({
        resumeList:[]
      })
    }
    var key = data.search
   
    request.get('vitae',data,{ noAuth : true}).then(res=>{
      console.log(res)
      if(res.data.length>0){
        var arrList = res.data
        if(key==''){
          for (var i=0; i<= 2;i++){
            arrList[i].birthday = this.getAge(arrList[i].birthday)
          }
        }
        that.setData({
          resumeList:that.data.resumeList.concat(arrList)
        })
      }else{
       that.setData({
         isPull:false
       })
      }
    })
  },
  getIndex:function(e){
    var that = this
    var index = e.detail.index
   if(index==1){
     if(that.data.userInfo){
      var isCompany = that.data.userInfo.is_company 
     }else{
      var isCompany = 0
     }
     
     if(isCompany==1){
      that.getServe()
      that.setData({
       activeIndex:index,
        keyValue:''
     })
     }else{
       console.log('企业')
      wx.showModal({
        title:'提示',
        content: '你当前还不是企业',
        success(res){
          if(res.confirm){
            wx.switchTab({
              url: '/pages/user/user',
            })
          }else{
           that.setData({
             activeIndex:0
            })
          }
        }
      })
     }
   }else{
    that.setData({
      activeIndex:index,
       keyValue:''
    })
  }
   
  },
  onLoadFun:function(s){
  console.log(s)
  },
  bindTextAreaBlur:function(e){
    var data = e.currentTarget.dataset.name
    this.setData({
      [data]:e.detail.value
    })
  },
  getValue:function(e){
   var data = e.currentTarget.dataset.name
   this.setData({
     [data]:e.detail.value
   })
  },
  needsSubmit:function(){
    var data = this.data
    console.log(data)
    var params = {
      name:data.name,
      phone:data.phone,
      company_name:data.companyName,
      type:data.type,
      number:data.number,
      benefit:data.benefit,
      position:data.position,
      ability:data.ability
    }
    request.post('employment',params,{ noAuth : true}).then(s=>{
      console.log(s)
      if(s.status==200){
        wx.navigateBack({
          delta: 1
        })
      }
    }).catch((err)=>{
      console.log(err)
      wx.showModal({
        content: '信息不能为空'
      })
    })
  },
  // 根据出生日期计算年龄周岁 传参格式为1996-06-08
 getAge(strBirthday) {
   console.log(strBirthday)
  var returnAge = '';
  var mouthAge = '';
  var strBirthdayArr = strBirthday.split("-");
  var birthYear = strBirthdayArr[0];
  var birthMonth = strBirthdayArr[1];
  var birthDay = strBirthdayArr[2];
  var d = new Date();
  var nowYear = d.getFullYear();
  var nowMonth = d.getMonth() + 1;
  var nowDay = d.getDate();
  if (nowYear == birthYear) {
    // returnAge = 0; //同年 则为0岁
    var monthDiff = nowMonth - birthMonth; //月之差 
    if (monthDiff < 0) {
    } else {
      mouthAge = monthDiff + '个月';
    }
  } else {
    var ageDiff = nowYear - birthYear; //年之差
    if (ageDiff > 0) {
      if (nowMonth == birthMonth) {
        var dayDiff = nowDay - birthDay; //日之差 
        if (dayDiff < 0) {
          returnAge = ageDiff - 1 + '岁';
        } else {
          returnAge = ageDiff + '岁';
        }
      } else {
        var monthDiff = nowMonth - birthMonth; //月之差 
        if (monthDiff < 0) {
          returnAge = ageDiff - 1 + '岁';
        } else {
          mouthAge = monthDiff + '个月';
          returnAge = ageDiff + '岁';
        }
      }
    } else {
      returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
    }
  }
  return returnAge + mouthAge; //返回周岁年龄+月份
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
   var v = this.data 
   var nextPage = ++v.pageSize
   console.log(v.isPull,nextPage)
   if(v.isPull){
     var data={
       page:nextPage,
       limit:v.pageNum,
       search:''
     }
     this.getConsult(data)
   }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})